package com.wn.icloud.controller;

import com.wn.icloud.common.exception.BaseException;
import org.apache.commons.lang3.StringUtils;

import java.util.Optional;

public class BaseCheckController {

    public <T> T checkNotNull(T reference) throws BaseException {
        if (reference == null) {
            throw new BaseException("Requested item wasn't found!");
        }
        return reference;
    }

    public <T> T checkNotNull(Optional<T> reference) throws BaseException {
        if (reference.isPresent()) {
            return reference.get();
        } else {
            throw new BaseException("Requested item wasn't found!");
        }
    }

    public void checkParameter(String name, String param) throws BaseException {
        if (StringUtils.isEmpty(param)) {
            throw new BaseException("Parameter '" + name + "' can't be empty!");
        }
    }

    public void checkArrayParameter(String name, String[] params) throws BaseException {
        if (params == null || params.length == 0) {
            throw new BaseException("Parameter '" + name + "' can't be empty!");
        } else {
            for (String param : params) {
                checkParameter(name, param);
            }
        }
    }
}
